import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 交易记录数据
  const transactions = [
    {
      id: 'TXN_20240120_001',
      orderId: 'ORDER_002',
      type: 'payment',
      typeName: '支付',
      amount: 6000,
      currency: 'CNY',
      status: 'completed',
      statusName: '已完成',
      method: 'alipay',
      methodName: '支付宝',
      payer: {
        id: 'buyer_2',
        name: '李四',
        avatar: '/images/users/user-2.jpg',
        account: 'li.si@example.com',
      },
      payee: {
        id: 'seller_1',
        name: '酒店管理集团',
        avatar: '/images/sellers/seller-1.jpg',
        account: 'contact@hotel-group.com',
      },
      description: '豪华行政套房1501预订款',
      listingTitle: '豪华行政套房1501 - 80平米，城市景观',
      platformFee: 180, // 3% 平台费
      sellerReceives: 5820,
      paymentDetails: {
        tradeNo: 'ALIPAY_20240120_15450001',
        bankName: '',
        cardNumber: '',
        verificationCode: 'VERIFIED',
      },
      createdAt: '2024-01-20T15:45:00Z',
      completedAt: '2024-01-20T15:45:30Z',
      notes: '支付成功，自动确认',
    },
    {
      id: 'TXN_20231225_002',
      orderId: 'ORDER_003',
      type: 'payment',
      typeName: '支付',
      amount: 90_000,
      currency: 'CNY',
      status: 'completed',
      statusName: '已完成',
      method: 'bank_transfer',
      methodName: '银行转账',
      payer: {
        id: 'buyer_3',
        name: '王五',
        avatar: '/images/users/user-3.jpg',
        account: 'wang.wu@hotel.com',
      },
      payee: {
        id: 'seller_2',
        name: '酒店科技公司',
        avatar: '/images/sellers/seller-2.jpg',
        account: 'sales@hotel-tech.com',
      },
      description: '酒店管理系统首期付款',
      listingTitle: '酒店管理系统 - SaaS服务，企业级',
      platformFee: 2700, // 3% 平台费
      sellerReceives: 87_300,
      paymentDetails: {
        tradeNo: '',
        bankName: '招商银行',
        cardNumber: '****1234',
        verificationCode: 'MANUAL_VERIFIED',
      },
      createdAt: '2023-12-25T10:00:00Z',
      completedAt: '2023-12-25T14:30:00Z',
      notes: '银行转账，手动确认',
    },
    {
      id: 'TXN_20231231_003',
      orderId: 'ORDER_004',
      type: 'payment',
      typeName: '支付',
      amount: 675_000,
      currency: 'CNY',
      status: 'completed',
      statusName: '已完成',
      method: 'bank_transfer',
      methodName: '银行转账',
      payer: {
        id: 'buyer_4',
        name: '赵六',
        avatar: '/images/users/user-4.jpg',
        account: 'zhao.liu@resort.com',
      },
      payee: {
        id: 'seller_3',
        name: '酒店管理咨询公司',
        avatar: '/images/sellers/seller-3.jpg',
        account: 'service@hotel-consulting.com',
      },
      description: '管理团队服务费最终结算',
      listingTitle: '专业酒店管理团队 - 15人，8年经验',
      platformFee: 20_250, // 3% 平台费
      sellerReceives: 654_750,
      paymentDetails: {
        tradeNo: '',
        bankName: '工商银行',
        cardNumber: '****5678',
        verificationCode: 'MANUAL_VERIFIED',
      },
      createdAt: '2023-12-31T16:00:00Z',
      completedAt: '2023-12-31T18:30:00Z',
      notes: '项目完成，最终结算',
    },
    {
      id: 'TXN_20240115_004',
      orderId: 'ORDER_006',
      type: 'refund',
      typeName: '退款',
      amount: 25_000,
      currency: 'CNY',
      status: 'completed',
      statusName: '已完成',
      method: 'alipay',
      methodName: '支付宝',
      payer: {
        id: 'seller_5',
        name: '设备租赁公司',
        avatar: '/images/sellers/seller-5.jpg',
        account: 'refund@equipment-rental.com',
      },
      payee: {
        id: 'buyer_6',
        name: '钱八',
        avatar: '/images/users/user-6.jpg',
        account: 'qian.ba@company.com',
      },
      description: '设备故障退款',
      listingTitle: '厨房设备租赁 - 商用级',
      platformFee: -750, // 退款时退还平台费
      sellerReceives: -24_250,
      refundReason: '设备故障无法正常使用',
      paymentDetails: {
        tradeNo: 'ALIPAY_REFUND_20240115_001',
        bankName: '',
        cardNumber: '',
        verificationCode: 'AUTO_REFUND',
      },
      createdAt: '2024-01-15T11:20:00Z',
      completedAt: '2024-01-15T11:25:00Z',
      notes: '设备故障，全额退款',
    },
    {
      id: 'TXN_20240118_005',
      orderId: 'ORDER_005',
      type: 'cancel_refund',
      typeName: '取消退款',
      amount: 0,
      currency: 'CNY',
      status: 'completed',
      statusName: '已完成',
      method: 'system',
      methodName: '系统处理',
      payer: {
        id: 'system',
        name: '系统',
        avatar: '/images/system.jpg',
        account: 'system@platform.com',
      },
      payee: {
        id: 'buyer_5',
        name: '孙七',
        avatar: '/images/users/user-5.jpg',
        account: 'sun.qi@company.com',
      },
      description: '订单取消，无需退款',
      listingTitle: '中餐厅 - 300平米，传统中式美食',
      platformFee: 0,
      sellerReceives: 0,
      cancelReason: '公司搬迁，不再需要此服务',
      paymentDetails: {
        tradeNo: '',
        bankName: '',
        cardNumber: '',
        verificationCode: 'NO_PAYMENT',
      },
      createdAt: '2024-01-18T14:30:00Z',
      completedAt: '2024-01-18T14:30:00Z',
      notes: '订单取消，未产生支付',
    },
    {
      id: 'TXN_20240121_006',
      orderId: 'ORDER_007',
      type: 'payment',
      typeName: '支付',
      amount: 12_000,
      currency: 'CNY',
      status: 'pending',
      statusName: '处理中',
      method: 'wechat_pay',
      methodName: '微信支付',
      payer: {
        id: 'buyer_7',
        name: '周九',
        avatar: '/images/users/user-7.jpg',
        account: 'zhou.jiu@example.com',
      },
      payee: {
        id: 'seller_6',
        name: '会议室服务商',
        avatar: '/images/sellers/seller-6.jpg',
        account: 'service@meeting-room.com',
      },
      description: '会议室租赁费用',
      listingTitle: '多功能会议室 - 50人容量',
      platformFee: 360, // 3% 平台费
      sellerReceives: 11_640,
      paymentDetails: {
        tradeNo: 'WECHAT_20240121_12000001',
        bankName: '',
        cardNumber: '',
        verificationCode: 'PROCESSING',
      },
      createdAt: '2024-01-21T10:15:00Z',
      completedAt: null,
      notes: '微信支付处理中',
    },
  ];

  // 过滤逻辑
  let filteredTransactions = transactions;

  if (query.type) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.type === query.type,
    );
  }

  if (query.status) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.status === query.status,
    );
  }

  if (query.method) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.method === query.method,
    );
  }

  if (query.orderId) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.orderId === query.orderId,
    );
  }

  if (query.payerId) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.payer.id === query.payerId,
    );
  }

  if (query.payeeId) {
    filteredTransactions = filteredTransactions.filter(
      (txn) => txn.payee.id === query.payeeId,
    );
  }

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredTransactions = filteredTransactions.filter(
      (txn) =>
        txn.id.includes(keyword) ||
        txn.orderId.includes(keyword) ||
        txn.description.includes(keyword) ||
        txn.payer.name.includes(keyword) ||
        txn.payee.name.includes(keyword),
    );
  }

  if (query.startDate) {
    const startDate = new Date(query.startDate as string);
    filteredTransactions = filteredTransactions.filter(
      (txn) => new Date(txn.createdAt) >= startDate,
    );
  }

  if (query.endDate) {
    const endDate = new Date(query.endDate as string);
    filteredTransactions = filteredTransactions.filter(
      (txn) => new Date(txn.createdAt) <= endDate,
    );
  }

  if (query.minAmount) {
    const minAmount = Number.parseInt(query.minAmount as string);
    filteredTransactions = filteredTransactions.filter(
      (txn) => Math.abs(txn.amount) >= minAmount,
    );
  }

  if (query.maxAmount) {
    const maxAmount = Number.parseInt(query.maxAmount as string);
    filteredTransactions = filteredTransactions.filter(
      (txn) => Math.abs(txn.amount) <= maxAmount,
    );
  }

  // 排序
  const sortBy = (query.sortBy as string) || 'createdAt';
  const sortOrder = (query.sortOrder as string) || 'desc';

  filteredTransactions.sort((a, b) => {
    let aValue, bValue;

    switch (sortBy) {
      case 'amount': {
        aValue = Math.abs(a.amount);
        bValue = Math.abs(b.amount);
        break;
      }
      case 'createdAt':
      default: {
        aValue = new Date(a.createdAt).getTime();
        bValue = new Date(b.createdAt).getTime();
        break;
      }
    }

    return sortOrder === 'asc' ? aValue - bValue : bValue - aValue;
  });

  // 分页
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredTransactions.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredTransactions.length,
      page,
      pageSize,
    },
    msg: '获取交易记录成功',
  };
});
